<?php

class Paramadmin_IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function getruleAction()
    {
        $start = $this->getRequest()->getParam('start');
        $limit = $this->getRequest()->getParam('limit');

        $mRule = new Paramadmin_Model_Rules();
        
        $data = $mRule->getrules($start, $limit);
      
        $msg = 1;
        if(is_array($data)){
            $success = true;
 
        }else{
            $msg = 'Error al consultar los datos';
            $success = false;
            $data = array();       
        }                          
        $response = array(
            'success' => $success,
            'msg' => $msg,
            'data' => $data,    
            'total' => $data
            
        );

       $this->_helper->json->sendJson($response);
      
    }

    public function saveruleAction()
    {
       $post = json_decode(stripcslashes($this->getRequest()->getParam('params')), true);
       $sRule = new Paramadmin_Form_RuleSave();
       $data = array();
       $msg = 1;
       $action = '';
       
       try{
           //Validación de la información enviada por post
           $post['idsystem'] = 'modificar';//esta linea se debe eliminar cuando se cuente con el nombre del workspace como parametro
           if(!$sRule->isValid($post))
               throw new Exception(json_encode($sRule->getMessages()));
           
           //variable de datos que se va a almacenar
           $data = $sRule->getValues();           
           
           //modelo de la tabla rule
           $mRule = new Paramadmin_Model_Rules();
           
            //validar acción crear o editar
            if(!$post['idrule']){
                $data['dinactive'] = null;
                
                $idrule = $mRule->insert($data);
                $data['idrule'] = $idrule;
                $data['dinactive'] = 'Activo';
                $action = 'create';
            }else{
                $update = $mRule->update($data, "idrule = ".$post['idrule']);
                $idrule = (!is_nan($update))?$post['idrule']:-1;
                $action = 'update';
            }
            
            $success = true;           
    }catch(Exception $e){
        $success = false;
        $msg = $e->getMessage();
    }
       
         $response = array(
            'success' => $success,
            'msg' => $msg,
            'data' => $data,    
            'total' => $data,
            'action' => $action
        );
        
        $this->_helper->json->sendJson($response);
    }

    public function activeinactiveAction()
    {
        $idrule = $this->getRequest()->getParam('idrule');
        $msg = 1;
        try{
           
            $sql = sprintf("UPDATE rule r set r.dinactive = CASE WHEN r.dinactive IS NULL THEN CURRENT_TIMESTAMP 
                            WHEN r.dinactive IS NOT NULL THEN NULL END
                            where r.idrule = %d", $idrule);

        $db = Zend_Registry::get('db2');
        $db->query($sql);
        $success = true;
           
        }catch(Exception $e){
            $success = false;
            $msg = $e->getMessage();

        }
        $response = array(
            'success' => $success,
            'msg' => $msg
        );
    
        $this->_helper->json->sendJson($response);
        
    }


}













